package com.project.collectAPI.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.collectAPI.dao.CurtaskDao;
import com.project.collectAPI.dao.FirstclassDao;
import com.project.collectAPI.entity.Curtask;
import com.project.collectAPI.service.CurtaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Curtask)表服务实现类
 *
 * @author makejava
 * @since 2023-09-10 00:17:48
 */
@Service("curtaskService")
public class CurtaskServiceImpl extends ServiceImpl<CurtaskDao, Curtask> implements CurtaskService {

    @Autowired
    CurtaskDao curtaskDao;

    @Override
    public void addTask(Curtask curtask) {
        Curtask taskData=curtaskDao.selectOne(new QueryWrapper<Curtask>().eq("taskindex", curtask.getTaskindex()).eq("taskname",curtask.getTaskname()));
        if(taskData==null){ //为空才插入新用户
            curtaskDao.insert(curtask);
        }
    }

    @Override
    public void updateTaskStatus(Curtask curtask) {
        Curtask taskData=curtaskDao.selectOne(new QueryWrapper<Curtask>().eq("id",curtask.getId()));
        if(taskData!=null){
            curtask.setId(taskData.getId());
            curtaskDao.updateById(curtask);
        }
    }

    @Override
    public void updateTaskStatus(int arrIndex,String status) {
        Curtask taskData=curtaskDao.selectOne(new QueryWrapper<Curtask>().eq("taskindex",arrIndex));
        if(taskData!=null){
            taskData.setId(taskData.getId());
            taskData.setStatus(status);
            curtaskDao.updateById(taskData);
        }
    }

    @Override
    public Curtask getCurTask() {
        Curtask TaskData=curtaskDao.selectOne(new QueryWrapper<Curtask>().eq("status","进行中"));
        return TaskData;
    }

    @Override
    public int getTaskNumber() {
        List<Curtask> ListDatas=curtaskDao.selectList(new QueryWrapper<Curtask>().eq("status","等待").or().eq("status","进行中"));
        return ListDatas!=null?ListDatas.size():0;
    }

    @Override
    public void deleteTask(int curtaksid) {
        curtaskDao.delete(new QueryWrapper<Curtask>().eq("id", curtaksid));
    }

    @Override
    public void deleteAllTask() {
        curtaskDao.delete(new QueryWrapper<Curtask>().isNotNull("id"));
    }
}

